package com.yugao.fintech.draper.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.draper.admin.convert.AppConvert;
import com.yugao.fintech.draper.admin.entity.AppInfo;
import com.yugao.fintech.draper.admin.mapper.AppInfoMapper;
import com.yugao.fintech.draper.admin.param.AppInfoQuery;
import com.yugao.fintech.draper.admin.param.AppInfoVO;
import com.yugao.fintech.draper.admin.service.AppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class AppInfoServiceImpl extends ServiceImpl<AppInfoMapper, AppInfo> implements AppInfoService {
    @Override
    public IPage<AppInfoVO> pageQuery(AppInfoQuery appInfoQuery) {
        LambdaQueryWrapper<AppInfo> wrapper = Wrappers.<AppInfo>lambdaQuery()
                .like(StrUtil.isNotBlank(appInfoQuery.getName()), AppInfo::getName, appInfoQuery.getName())
                .like(StrUtil.isNotBlank(appInfoQuery.getCode()), AppInfo::getCode, appInfoQuery.getCode())
                .eq(Objects.nonNull(appInfoQuery.getId()), AppInfo::getId, appInfoQuery.getId())
                .orderByAsc(AppInfo::getSort);
        Page<AppInfo> pageInfo = baseMapper.selectPage(Page.of(appInfoQuery.getPageNum(), appInfoQuery.getPageSize()), wrapper);
        return AppConvert.INSTANCE.convertPage(pageInfo);
    }

    @Override
    public Boolean addApp(AppInfoVO appInfoVO) {
        AppInfo appInfo = AppConvert.INSTANCE.convert(appInfoVO);
        return baseMapper.insert(appInfo) > 0;
    }

    @Override
    public Boolean updateApp(AppInfoVO appInfoVO) {
        AppInfo appInfo = AppConvert.INSTANCE.convert(appInfoVO);
        return baseMapper.updateById(appInfo) > 0;
    }

    @Override
    public AppInfoVO findById(Long id) {
        AppInfo appInfo = baseMapper.selectById(id);
        return AppConvert.INSTANCE.convert(appInfo);
    }

    @Override
    public List<AppInfoVO> findAll() {
        List<AppInfo> appList = baseMapper.selectList(Wrappers.<AppInfo>lambdaQuery().orderByAsc(AppInfo::getSort));
        return AppConvert.INSTANCE.convertList(appList);
    }

    @Override
    public Boolean deleteByAppId(Long id) {
        return baseMapper.deleteById(id) > 0;
    }
}